package gui;

import javax.swing.*;
import javax.swing.table.*;

import model.EstateCollection;
import model.Road;
import model.RoadCollection;
import database.CustomerOperations;
import database.DailyOrderOperations;
import database.EstateOperations;
import database.OrderItemOperations;
import database.OrderOperations;
import database.RoadOperations;

import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

@SuppressWarnings("serial")
public class roadInputGUI extends JFrame implements ActionListener,ItemListener 
{
	
	private JPanel p1, p15, p2, p3,p4, p5;
	private JLabel l1,l15, l2, l3;
	private JButton back, next, add ,remove;
	private JTextField f2;
	private JComboBox <String> box;
	private RoadOperations ro;
	private RoadCollection rcoll;
	private CustomerOperations co;
	private DailyOrderOperations doo;
	private OrderOperations oo;
	private OrderItemOperations oio;
	private JTable table;
	private DefaultTableModel model;
	private ResultSet rs;
	private EstateOperations eo;
	private String estName;
	private String newName;
	ArrayList<String> ests;
	private Vector<Object> row;
	
	public roadInputGUI(RoadOperations r,RoadCollection rc,EstateOperations e) 
	{
		super("Road Input Screen");
		this.setSize(840, 500);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.ro=r;
		this.rcoll=rc;
		this.eo=e;

		ro.dropRoadTable();
		ro.CreateRoadTable();
		
		//Panel 1 
		p1 = new JPanel();
		p1.setPreferredSize(new Dimension(800, 40));
		l1 = new JLabel("Enter all your Roads here");
		p1.add(l1);
		
		//Panel 1.5 
		p15 = new JPanel();
		p15.setPreferredSize(new Dimension(800, 40));
		l15 = new JLabel("Roads");
		l15.setFont(new Font("serif",Font.BOLD,26));
		p15.add(l15);
		
		
		//Panel 2
		p2 = new JPanel();
		p2.setLayout(new GridLayout(1,4));
		p2.setPreferredSize(new Dimension(600, 20));
		
		//list of estates in combobox
		box = new JComboBox<String>();	
		ests=new ArrayList<String>();
		ests=eo.getEstateNames();
		for(int i=0;i<ests.size();i++)
		{
			box.addItem(ests.get(i));
		}
		box.addItemListener(this);
		estName=(String)box.getSelectedItem();
		
		l2 = new JLabel("Choose Estate: ", JLabel.CENTER);
		l3 = new JLabel("Road name: ", JLabel.CENTER);
		
		f2 = new JTextField(25);
		p2.add(l2);
		p2.add(box);
		p2.add(l3);
		p2.add(f2);
		
		//Panel 3
		p3 = new JPanel();
		p3.setPreferredSize(new Dimension(800, 80));
		add = new JButton("ADD");
		add.addActionListener(this);
		remove = new JButton("REMOVE");
		remove.addActionListener(this);
		p3.add(add);
		p3.add(Box.createRigidArea(new Dimension(200,0)));
		p3.add(remove);
		
		//Panel 4
		p4 = new JPanel();
		p4.setPreferredSize(new Dimension(800, 200));
		
		// create blank JTable on screen
		String[] cols={"ID","ROAD NAME","ESTATE"};
		int numRows=100;
		model=new DefaultTableModel(numRows,cols.length);
		model.setColumnIdentifiers(cols);
		table = new JTable(model);
		table.setFillsViewportHeight(true);
		table.setAutoCreateColumnsFromModel(false);
		JScrollPane sp=new JScrollPane(table);
		sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		sp.setPreferredSize(new Dimension(700, 190));
				
		//adjust size of columns on table
		TableColumnModel tbm=table.getColumnModel();
		tbm.getColumn(0).setMaxWidth(30);
		model.setRowCount(0);
		p4.add(sp,BorderLayout.CENTER);
		
		//Panel 5
		p5 = new JPanel();
		p5.setPreferredSize(new Dimension(800, 32));
		back = new JButton("BACK");
		back.addActionListener(this);
		next = new JButton("NEXT");
		next.addActionListener(this);
		p5.add(back);
		p5.add(Box.createRigidArea(new Dimension(650,0)));
		p5.add(next);
		
		//Adding Panels to the page
		this.add(p1, BorderLayout.PAGE_START);
		this.add(p15, BorderLayout.NORTH);
		this.add(p2, BorderLayout.NORTH);
		this.add(p3, BorderLayout.CENTER);
		this.add(p4, BorderLayout.SOUTH);
		this.add(p5, BorderLayout.PAGE_END);
	}
	
		
		//add road to database and array
			public void addRoad()
			{
				if(f2.getText().equals(""))
				{
					JOptionPane.showMessageDialog(null, "You must enter data in the road name field","ERROR",JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					String name=f2.getText();
					int eid=getEstId(estName);
					Road r = new Road(eid,name);
					ro.addRoad(r);
					rcoll.addRoad();
					f2.setText("");
				}
		}
	
		// get the estate id from its name
		public int getEstId(String name)
		{
			rs=eo.getEstIdName();
			int a=-1;
			try {
				while(rs.next())
				{
					if(rs.getString(1).equals(name))
					{
						a=rs.getInt(2);
					}
				}
			} catch (SQLException e) 
			{
				e.printStackTrace();
			}
			return a;
		}
		
		// get the estate name from its id
			public String getEstName(int i)
				{
					rs=eo.getEstIdName();
					String a=null;
					try {
						while(rs.next())
						{
							if(rs.getInt(2)==i)
							{
								a=rs.getString(1);
							}
						}
					} catch (SQLException e) 
					{
						e.printStackTrace();
					}
					return a;
					
				}
		
			//remove road from database and array
			public void removeRoad()
			{
				String a;
				a=JOptionPane.showInputDialog(null, "Enter the ID of the road you wish to remove","Remove Road",JOptionPane.QUESTION_MESSAGE);
				if(a==null)
				{
					//do nothing
				}
				else
				{
					int b=Integer.parseInt(a);
					boolean ans=rcoll.removeRoad(b);
					if(ans==true)
					{
						JOptionPane.showMessageDialog(null, "The road has been deleted","Deleted",JOptionPane.WARNING_MESSAGE);
					}
					else
					{
						JOptionPane.showMessageDialog(null, "The road you choose is not a valid road","Not Deleted",JOptionPane.WARNING_MESSAGE);
					}
				}
			}
			
			// update the table after every new road added or deleted
				public void updateTable()
				{
					
					int counter=0;
					model.setRowCount(0);
					try {
						ResultSet rs =ro.getRoad();
						while(rs.next())
						{
							row=new Vector<Object>();
							row.add(rs.getInt(1));
							row.add(rs.getString(3));
							row.add(getEstName(rs.getInt(2)));
							model.insertRow(counter,row);
							counter++;
						}
					} catch (SQLException e) 
					{
						e.printStackTrace();
					}
					
				}
	
			public static void main(String args[]) 
			{
				RoadOperations ro=new RoadOperations();
				RoadCollection r=new RoadCollection(ro);
				EstateOperations e=new EstateOperations();
				roadInputGUI ri = new roadInputGUI(ro,r,e);
				ri.setVisible(true);
			}
	
			@Override
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==add)
		{
			addRoad();
			updateTable();
		}
		if(ae.getSource()==remove)
		{
			removeRoad();
			updateTable();
		}

		if(ae.getSource()==next)
		{
			eo.closeDB();
			try
			{
				ro.closeDB();
			}
			catch(Exception e)
			{
				System.out.println("could not close ro");
			}
			
			//create customer table
			co=new CustomerOperations();
			co.dropCustomerTable();
			co.CreateCustomerTable();
			co.closeDB2();
			
			//create order table
			oo=new OrderOperations();
			oo.dropOrderTable();
			oo.CreateOrderTable();
			oo.closeDB2();
			
			//create day table
			doo=new DailyOrderOperations();
			doo.dropDayTable();
			doo.CreateDayTable();
			doo.closeDB2();
			
			//create order item table
			oio=new OrderItemOperations();
			oio.dropOrderItemTable();
			oio.CreateOrderItemTable();
			oio.closeDB2();
			
			//create suspend table
			
			loginGUI log=new loginGUI();
			log.setVisible(true);
			this.setVisible(false);
		}
		if(ae.getSource()==back)
		{
			eo.closeDB();
			ro.closeDB();
			EstateOperations eo=new EstateOperations();
			EstateCollection ec=new EstateCollection(eo);
			deliveryInputGUI dig=new deliveryInputGUI(eo,ec);
			dig.setVisible(true);
			this.setVisible(false);
		}
		
	}

	@Override
	public void itemStateChanged(ItemEvent ae)
	{
		if (ae.getSource() == box && ae.getStateChange() == ItemEvent.SELECTED)
		{
			estName=(String)box.getSelectedItem();
		}
		
	}
}

